package com.android.cl.coludtf.activity.base;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.util.LinkedList;
import java.util.List;

public class BaseActivity extends AppCompatActivity {

    public Gson gson = new Gson();
    /**
     * 记录处于前台的Activity
     */
    private static BaseActivity mForegroundActivity = null;
    /**
     * 记录所有活动的Activity
     */
    private static final List<BaseActivity> mActivities = new LinkedList<BaseActivity>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        registerBroadcast();
    }

    //退出时的时间
    private long mExitTime;

    //对返回键进行监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 1500) {
            Toast.makeText(this, "再按一次退出当前应用程序", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }

    /**
     * 退出应用
     */
    public void exitApp() {
//        finishAll();
        android.os.Process.killProcess(android.os.Process.myPid());
    }

//    /**
//     * 广播接收者
//     */
//    MyReceiver receiver;
//
//    private void registerBroadcast() {
//        // 注册广播接收者
//        receiver = new MyReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.addAction(ConstantUtils.EXIT_LOGIN);
//        MyAppcliation.context.registerReceiver(receiver, filter);
//    }
//
//    class MyReceiver extends BroadcastReceiver {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if (intent.getAction().equals(ConstantUtils.EXIT_LOGIN)) {
//                MyLog.e(ConstantUtils.EXIT_LOGIN, "退出登陆");
//                finish();
//            }
//        }
//    }
}
